'''

get the mean brightness of all the image
'''
from PIL import Image,ImageStat,ImageEnhance
import math
import os
from tqdm import tqdm

def get_image_brightness(dst_src):
    img=Image.open(dst_src)
    img_bright = ImageEnhance.Brightness(img)
    img = img_bright.enhance(10)
    return img

def get_image_light_mean(dst_src):
    im = Image.open(dst_src).convert('L')
    stat = ImageStat.Stat(im)
    return stat.mean[0]


def get_image_light_mean_sqrt(dst_src):
    # im = Image.open(dst_src)
    im=get_image_brightness(dst_src)
    stat = ImageStat.Stat(im)
    r, g, b = stat.mean
    return math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))

def get_image_light_gs(dst_src):
    # im = Image.open(dst_src)
    im=get_image_brightness(dst_src)
    stat = ImageStat.Stat(im)
    gs = (math.sqrt(0.241 * (r ** 2) + 0.691 * (g ** 2) + 0.068 * (b ** 2))
          for r, g, b in im.getdata())
    return sum(gs) / stat.count[0]

brightness=[]

for image in tqdm(os.listdir("Carplate")):
    try:
        brightness.append(get_image_light_gs("Carplate"+"/"+image))
    except:
        print(image)
        continue

temp=0
for br in brightness:
    temp+=br
print(temp/len(brightness))